package com.datamation.swadeshi.view;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.graphics.Typeface;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.WindowManager;
import android.widget.EditText;
import android.widget.TextView;

import com.datamation.swadeshi.R;

public class SplashActivity extends Activity implements OnClickListener {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); 
		setContentView(R.layout.splash_activity);
		
		try {
			titleMethod();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		
		//checking ip address of the server
		alertDialogbox(SplashActivity.this);

        
	}
	
	/**
	 * Title name method
	 */
	public void titleMethod() throws Exception{
			Typeface tf = Typeface.createFromAsset(getAssets(),"fonts/fat_tats.ttf");
		 	TextView tvTitle = (TextView) findViewById(R.id.spl_title);
		 	TextView tvVersion = (TextView) findViewById(R.id.spl_version);
	        tvTitle.setText("Swadeshi");
	        tvTitle.setTypeface(tf);
	        
	        PackageInfo pInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
	        String version = pInfo.versionName;
	        tvVersion.setText("Version: "+version);
	        
	}
	
	/**
	 * IP alert dialogbox
	 */
	private void alertDialogbox(final Context context){
		// get prompts.xml view
		LayoutInflater layoutInflater = LayoutInflater.from(context);

		View promptView = layoutInflater.inflate(R.layout.ip_connection, null);

		AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
		alertDialogBuilder.setTitle("Please enter server URL");
		// set prompts.xml to be the layout file of the alertdialog builder
		alertDialogBuilder.setView(promptView);

		final EditText input = (EditText) promptView.findViewById(R.id.et_ip);

		// setup a dialog window
		alertDialogBuilder
				.setCancelable(false)
				.setPositiveButton("OK", new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog, int id) {

								new PrefetchData().execute();
								
								
							}
						})
				.setNegativeButton("Cancel",
						new DialogInterface.OnClickListener() {
							public void onClick(DialogInterface dialog,	int id) {
								dialog.cancel();
								
								SplashActivity.this.finish();
							}
						});

		// create an alert dialog
		AlertDialog alertD = alertDialogBuilder.create();

		alertD.show();
	}
	
	
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub

	}
	
	
	 @Override
	protected void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		 finish();
		
	}


	/**
     * Async Task to make http call
     */
	 ProgressDialog progressDialog;
    private class PrefetchData extends AsyncTask<Void, Void, Void> {
 
        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // before making http calls         
            progressDialog = ProgressDialog.show(SplashActivity.this, "Wait", "Downloading...");
        }
 
        @Override
        protected Void doInBackground(Void... arg0) {
            
        	try {
        		 Thread.sleep(4000);
        		
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
 
            return null;
        }
 
        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            progressDialog.dismiss();
           // Intent mainActivity = new Intent(SplashActivity.this, MainActivity.class);SettingsActivity
            Intent mainActivity = new Intent(SplashActivity.this, SettingsActivity.class);
	        startActivity(mainActivity);
          
           
        }
 
    }
}
